//
//  SEGAuthManagerViewManagerCell.m
//  SEGUhomePropertyBaseService
//
//  Created by 揭婧雯 on 2018/8/6.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGAuthManagerViewManagerCell.h"
@interface SEGAuthManagerViewManagerCell ()
/**
 背景
 */
@property (nonatomic, strong)UIView *bgView;

/**
 房屋标签
 */
@property (nonatomic, strong)UILabel *houseNameLabel;

/**
 描述标签
 */
@property (nonatomic, strong)UILabel *descLabel;

/**
 分割线
 */
@property (nonatomic, strong)UIView *lineView;

@property (nonatomic, strong)UIButton *rightArrowButton;

@end

@implementation SEGAuthManagerViewManagerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    UIView *bgView = [[UIView alloc] init];
    [self.contentView addSubview:bgView];
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 5;
    self.bgView = bgView;

    self.houseNameLabel = [[UILabel alloc] init];
    self.houseNameLabel.font = [UIFont boldSystemFontOfSize:20];
    self.houseNameLabel.textAlignment = NSTextAlignmentLeft;
    self.houseNameLabel.numberOfLines = 0;
    [self.bgView addSubview:self.houseNameLabel];

    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.font = kFont_13;
    descLabel.textAlignment = NSTextAlignmentLeft;
    [self.bgView addSubview:descLabel];
    self.descLabel = descLabel;
    
    self.rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.bgView addSubview:self.rightButton];
    self.rightButton.titleLabel.font = kFont_11;
    
    [self.houseNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).mas_offset(15);
        make.right.equalTo(self.rightButton.mas_left).offset(-10);
        make.top.equalTo(self.bgView).offset(18);
    }];
    
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).mas_offset(15);
//        make.right.equalTo(self.rightButton.mas_left).offset(0);
        make.top.equalTo(self.houseNameLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.bgView.mas_bottom).offset(-19);
    }];

    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.bgView);
        make.right.equalTo(self.bgView).offset(-15);
    }];

    self.setDefaultButton.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    [self.setDefaultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(0);
//        make.right.equalTo(self.bgView).offset(0);
        make.top.equalTo(self.descLabel.mas_bottom).offset(19);
        make.height.mas_equalTo(44);
    }];

    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(15);
        make.right.equalTo(self.bgView).offset(-15);
        make.top.equalTo(self.descLabel.mas_bottom).offset(19);
        make.height.mas_equalTo(0.5);
    }];

    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(self.contentView).offset(10);
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
            make.bottom.equalTo(self.contentView.mas_bottom).priorityHigh();
        }
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker* make) {
        // iOS10 以上的适配
        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 10.0) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self.bgView.mas_bottom);
        }else{
            make.edges.equalTo(self);
        }
    }];
    
    [self.bgView addSubview:self.rightArrowButton];
    [self.rightArrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.top.equalTo(self.lineView.mas_top).dividedBy(2).mas_offset(-self.rightArrowButton.imageView.image.size.height*0.5);
    }];
}

- (void)setUpManagerCellWithModel:(SEGHouseInfoModel *)model
{
    self.houseNameLabel.text = model.houseName;
    //房屋申请状态
    UIColor *color;
    NSString *str = @"";
    NSString *buttonText = @"";
    SEGHouseAuditStatus status = [model.authFlag integerValue];
    if (status == SEGHouseAuditStatus_Pass) {
        self.bgView.backgroundColor = kColor_T;
        color = kColor_W;
        //描述和按钮逻辑
        str = [NSString stringWithFormat:@"已有%@名成员",model.userCount];
        [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bgView.mas_bottom).offset(- 19 - 44);
        }];
        
        //右侧按钮逻辑
        buttonText = @"";
        self.rightButton.layer.masksToBounds = NO;
        self.rightButton.layer.borderWidth = 0;
        
        //默认房屋
        self.lineView.hidden = NO;
        self.setDefaultButton.hidden = NO;
        
        //是否是默认的房屋 0不是，1是
        BOOL isDefault = [model.defaultFlag isEqualToString:@"1"];
        self.setDefaultButton.selected = isDefault;
//        self.setDefaultButton.userInteractionEnabled = !isDefault;
        self.rightArrowButton.hidden = NO;
        [self.rightButton setHidden:YES];
        
        [self.houseNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(self.rightArrowButton.mas_left).offset(-10);
            
        }];
        
    }else {
        self.bgView.backgroundColor = kColor_L3;
        color = kColor_2;
        CGFloat rightButtonWidth = 0;
        if (status == SEGHouseAuditStatus_Apply) {
            str = @"审核中（询问物业，了解进度）";
            buttonText = @"  致电管理处  ";
            rightButtonWidth = 67;
        }else if (status == SEGHouseAuditStatus_Refuse) {
            str = @"审核不通过（与预留信息不一致）";
            buttonText = @"  重新申请  ";
            rightButtonWidth = 57;
        }
        [self.descLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bgView.mas_bottom).offset(-19);
        }];
        
        [self.rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.bgView);
            make.right.equalTo(self.bgView).offset(-15);
            make.height.mas_equalTo(19);
            make.width.mas_equalTo(rightButtonWidth);
        }];
        self.rightButton.layer.masksToBounds = YES;
        self.rightButton.layer.cornerRadius = 9.5;
        self.rightButton.layer.borderWidth = 0.5;
        self.rightButton.layer.borderColor = kColor_2.CGColor;
        self.rightArrowButton.hidden = YES;
        [self.rightButton setHidden:NO];
        self.lineView.hidden = YES;
        self.setDefaultButton.hidden = YES;
        [self.houseNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.rightButton.mas_left).offset(-10);
        }];
    }
    
    self.descLabel.text = str;
    [self.rightButton setTitle:buttonText forState:UIControlStateNormal];
    
    self.houseNameLabel.textColor = color;
    self.descLabel.textColor = color;
    [self.rightButton setTitleColor:color forState:UIControlStateNormal];
    [self layoutSubviews];//解决房屋名称多行时布局出错问题
}

- (SEGAdjustableButton *)setDefaultButton
{
    if (!_setDefaultButton) {
        _setDefaultButton = [[SEGAdjustableButton alloc] init];
        [_setDefaultButton setTitle:@"设为默认房屋" forState:UIControlStateNormal];
        [_setDefaultButton setTitleColor:kColor_W forState:UIControlStateNormal];
        _setDefaultButton.titleLabel.font = kFont_15;
        [_setDefaultButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"houseBtn_list_select_nor"]) forState:UIControlStateNormal];
        [_setDefaultButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"houseBtn_list_select"]) forState:UIControlStateSelected];
        [_setDefaultButton layoutButtonWithEdgeInsetsStyle:SEGButtonEdgeInsetsStyleLeft imageTitleSpace:6];
         _setDefaultButton.contentHorizontalAlignment= UIControlContentHorizontalAlignmentLeft;
        [self.bgView addSubview:_setDefaultButton];
    }
    return _setDefaultButton;
}

         
- (UIView *)lineView
{
    if(!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kColor_W;
        _lineView.alpha = 0.35;
        [self.bgView addSubview:_lineView];
    }
    return _lineView;
}
         
         -(UIButton *)rightArrowButton{
             if (!_rightArrowButton) {
                 _rightArrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
                 [_rightArrowButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_pocketmoney_arrow_w")] forState:UIControlStateNormal];
                 [_rightArrowButton sizeToFit];
                 _rightArrowButton.tintColor = kColor_W;
             }
             return _rightArrowButton;
         }
         
@end
         
